import socket
import threading


def response(csock, caddr):
    while True:
        data = csock.recv(4096)
        print(data.decode())
        if not data:
            csock.close()
            break
        data = b'received: ' + data
        csock.send(data)


def run():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(addr)
    sock.listen(backlog)
    while True:
        csock, caddr = sock.accept()
        t = threading.Thread(target=response, args=(csock, caddr))
        t.start()


backlog = 5
addr = ('', 3001)


if __name__ == '__main__':
    run()